/*
 *  Copyright (C) 2010 Glauco
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.common.defaults;

import java.util.logging.Level;
import org.balisunrise.application.*;
import org.balisunrise.common.*;

/**
 *
 * @author glauco
 */
public class VEAUtil {

    public static VisualEntity getTopVisualEntity(VisualEntity ve){

        if(ve == null)
            return null;

        VisualEntity aux = ve;
        
        while(aux != null){
            ve = aux;
            aux = null;
            Basic b = ve.getOwner();
            if(b instanceof VisualEntity)
                aux = (VisualEntity) b;
        }

        return ve;
    }

    public static VisualEntity getVisualEntity(Basic b){

        Basic aux = b;
        VisualEntity ve = null;

        while(ve == null){

            if(aux == null)
                return null;

            if(aux instanceof VisualEntity)
                ve = (VisualEntity) aux;
            
            aux = aux.getOwner();
        }

        return ve;
    }

    public static VisualEntity getTopVisualEntity(Basic b){

        VisualEntity ve = getVisualEntity(b);

        return getTopVisualEntity(ve);
    }

    public static Messenger getMessenger(Basic b){

        VisualEntity ve = getVisualEntity(b);

        while(ve != null){

            if(ve instanceof Messenger)
                return (Messenger) ve;
            if(ve instanceof Editor)
                return ((Editor) ve).getMessenger();
            if(ve instanceof Transaction)
                return ((Transaction) ve).getEditor().getMessenger();

            Basic baux = ve.getOwner();
            if(baux instanceof VisualEntity)
                ve = (VisualEntity) baux;
            else
                ve = null;
        }

        return null;
    }

    public static Messenger getMessenger(){
        try{
            WindowsManager vm = Application.get().getWindowsManager();

            VisualEntity ve = vm.getActiveWindow().getVisualEntity();

            return getMessenger(ve);

        }catch(Exception ex){
            Application.get().getLogger().log(Level.INFO,
                    "Messenger Inacessível", ex);
        }

        return null;
    }

    public static DAOProvider getDAOProvider(Basic b){

        if (b == null)
            return null;

        if (b instanceof DAOProvider)
            return (DAOProvider) b;

        return getDAOProvider(b.getOwner());
    }

    public static Window getWindow(Basic b){

        if(b == null)
            return null;

        if(b instanceof Window)
            return (Window) b;

        return getWindow(b.getOwner());
    }

    public static Basic getFromBasic(Basic source, Class type){

        if(source == null)
            return null;

        if(type.isAssignableFrom(source.getClass()))
            return source;

        if (source.getClass().isAssignableFrom(type))
            return source;

        Basic owner = getFromBasic(source.getOwner(), type);

        if(owner != null)
            return owner;

        for (Basic child : source.getChildren().getBasics()) {
            if(child.getClass().isAssignableFrom(type))
                return child;
        }

        return null;
    }
}
